Zhiyong Zhang's Psychometric Website
 
Home Control Panel Login Register Search Submit Logout About me Contact me
My Research
Google
Web
This Site
Home > SAS power analysis Macros for a linear growth curve model
SAS power analysis Macros for a linear growth curve model
2009-04-15    Zhang, Z. & Wang, L.       Read: 15497 times
Cite this page: Zhang, Z. & Wang, L. (2009). SAS power analysis Macros for a linear growth curve model. Retrieved November 21, 2024, from https://www.psychstat.org/us/article.php/78.htm.
SAS Macros for a linear growth curve model
These macros are licensed under the GNL General Public License Version 2.0. You can use/modify/distribute those macros. For questions or comments, please contact Johnny Zhang at zhiyongzhang(@)nd.edu. It would be appreciated if you cite the macros in the following way:

Zhang, Z., & Wang, L. (2009). Power analysis for growth curve models using SAS. Behavior Research Methods, 41(4), 1083-1094. Request a copy

/*Suppress the output and the log */
options nosource nonotes nosource2 nomprint;

/*Power analysis of growth curve models using SAS*/
/*CHANGE THE PARAMETERS HERE*/
*model parameters;
%LET MuL=10;        *mean level/initial status;
%LET MuS=.2;       *mean slope/rate of change;
%LET Sigma_e=1;    *residual standard deviation;
%LET Sigma_L=2;    *level standard deviation;
%LET Sigma_S=1;    *slope standard deviation;
%LET rho=0;        *correlation between levels and slopes;
%LET miss=0;       *missing data rate, 0: no missing data;
*power parameters;
%LET R=1000;       *number of simulation replications;
%LET T=5;          *number of measurement occasions;
%LET start=50;     *the minimum sample size to consider;
%LET end=100;     *the maximum sample size to consider;
%LET step=50;      *the step between two sample sizes;
%LET df=1;         *the difference in the numbers of parameters;
%LET seed=123;     *random number generator seed;

/*DO NOT CHANGE CODES BELOW UNLESS YOU KNOW WHAT YOU ARE DOING*/

/*The first Macro: LL*/
/*Calculate the chi-square difference of two nested growth curve models*/
%MACRO LL(N,T,seed);
DATA Sim_LinGM;
  N = &N; seed = &seed;
* setup arrays so that we can include multiple variables for repeated measures;
  ARRAY y_score{&T} y1-y&T;
  ARRAY M{&T} m1-m&T;
  m1=1;
* generate raw data with considering the missing data rate;
  DO _N_ = 1 TO N;
    e_L=RANNOR(seed);
    e_S=&rho*e_L+SQRT(1-&rho**2)*RANNOR(seed);
    L_score=&MuL+&Sigma_L*e_L;
    S_score=&MuS+&Sigma_S*e_S;
    DO t = 1 TO &T;
      y_score{t} = L_score + (t-1) * S_score + &Sigma_e*RANNOR(seed);
    END;
* include indicator variables to generate missing data;
 DO t=2 TO &T;
   m{t}=m{t-1};
   IF m{t-1}=1 AND  RANUNI(seed) > (1-&miss * (t-1))/(1-&miss * (t-2))
      THEN m{t} = 0;
      IF m{t}=0  THEN y_score{t}=.;
 END;
    KEEP y1-y&T;
    OUTPUT;
    END;
RUN;
DATA LinGM;
  SET Sim_LinGM;
  %DO t = 1 %TO &T;
    id = _N_; time=&t-1; y=y&t; OUTPUT;
  %END;
  KEEP id time y;
RUN;

/*Fit two nested models to the data*/
ODS OUTPUT FitStatistics(persist=proc)=fit;
*Model 1: the true model;
TITLE1 'Linear Growth Model';
PROC MIXED DATA=LinGM NOCLPRINT COVTEST MAXITER=100 METHOD=ML;
  CLASS id;
  MODEL y = time  /SOLUTION DDFM=BW CHISQ;
  RANDOM INTERCEPT time / SUBJECT=id TYPE=UN GCORR;
RUN;

*Model 2: mean slope = 0;
PROC MIXED DATA=LinGM NOCLPRINT COVTEST MAXITER=100 METHOD=ML;
  CLASS id;
  MODEL y =    /SOLUTION DDFM=BW CHISQ;
  RANDOM INTERCEPT time / SUBJECT=id TYPE=UN GCORR;
RUN;

ODS OUTPUT CLOSE;
%MEND LL;

/*The second Macro: POWER*/
/*This Macro calls the first Macro LL for each replication*/
* Calculate power based on R replications;
%MACRO POWER(R,N,T,seed,df);
DATA tempfit;
  DO _N_=1 TO 8;
    tempfit=_N_;
 OUTPUT;
  END;
RUN;
 
%LL(&N,&T,&seed);
DATA fit;
  MERGE fit tempfit;
RUN;

DATA allfit;
  SET fit;
RUN;

%DO I = 2 %TO  &R;
  PROC DATASETS LIBRARY=WORK; DELETE fit; RUN; QUIT;
  %LL(&N,&T,%eval(&seed+&I*1389));
  DATA fit;
    MERGE fit tempfit;
  RUN;

  DATA allfit;
    SET allfit fit;
  RUN;
  DM 'CLEAR LOG';
%END;

DATA allfit;
  SET allfit;
  IF MOD(_N_,4) ~= 1 THEN DELETE;
  KEEP Value;
RUN;

DATA allfit;
  SET allfit;
  id =INT((_N_-.1)/2)+1;
  modelnum = MOD(_N_+1, 2);
RUN;

PROC TRANSPOSE DATA=allfit OUT=allfit prefix=model;
  BY id;
  ID modelnum;
  VAR Value;
RUN;

DATA allfit;
  SET allfit;
  ss = &N;
  diff = model1 - model0;
  ind = 1;
  IF diff=. THEN DELETE;
  IF diff<0 THEN DELETE;
  IF diff < CINV(.95, &df) THEN ind = 0;
  DROP id _NAME_ model0 model1;
RUN;


PROC MEANS DATA = allfit;
  VAR ss ind;
  OUTPUT OUT=power mean(ss ind)=ss power;
RUN;

%MEND POWER;

/*The third Macro: POWERCURVE*/
/* This Macro calls the second Macro for each sample size*/
%MACRO powercurve(R, seed, st, end, step, T,df);
%POWER(&R, &st, &T, %eval(&seed+&st), &df);
DATA allpower;
  SET power;
  RUN;

%LET st = %eval(&st + &step);
%DO %WHILE (&st <=  &end);
  %POWER(&R, &st, &T, %eval(&seed+&st), &df);
  %LET st = %eval(&st + &step);
  DATA allpower;
    SET allpower power;
  RUN;
  DM 'CLEAR LOG';
%END;

* Save the results for possible future use;
DATA allpower;
  SET allpower;
  FILE "power.txt";
  PUT ss power;
RUN;

* Plot the power curve;
ODS PDF FILE='power.pdf' NOTOC;
PROC GPLOT DATA = allpower;
  SYMBOL I=JOIN;
  PLOT power*ss;
RUN;
QUIT;
ODS PDF CLOSE;
%MEND powercurve;

ODS RESULTS OFF;
ODS LISTING CLOSE;
%powercurve(&R,&seed,&start,&end,&step,&T,&df);
ODS RESULTS ON;
ODS LISTING;

Submitted by: johnny
Add a comment View comment Add to my favorite Email to a friend Print
If you want to rate, please login first, or click here to register. Or you can use USERNAME: guest and PASSWORD: guest to log in.
Average score 0, based on 0 comments
1 2 3 4 5 6 7 8 9 10
Latest comment (Total: 2 ) Time Author Reply
Try to run the whole macros without changing 2010-08-10 13:20 pm johnny 0
Problem with macro 2010-04-11 15:32 pm guest 0
More comments...

Copyright © 2003-13 Zhiyong Zhang \'s Psychometric Website
Maintained by Zhiyong Zhang